package Com.CXSH.Guanwan.controller;
import Com.CXSH.Guanwan.Mapper.UserMapper;
import Com.CXSH.Guanwan.config.JWTUtil;
import Com.CXSH.Guanwan.config.Result;
import Com.CXSH.Guanwan.pojo.User;
import Com.CXSH.Guanwan.service.loginService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;


import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
public class LoginController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private loginService userService;

    /**
     * 登录
     * */
    @PostMapping("/login")
    public Result login(@RequestParam String username, @RequestParam String password) throws UnsupportedEncodingException {
        User user=userService.getUserByPass(username, password);
        if(user == null){
            return Result.error("用户名或密码错误");
        }
        String type = user.getType();
        //登录成功则调用JWTUtil类的创建Token方法返回客户端
        String token= JWTUtil.createToken(user);
        return Result.ok(token, type);
    }
   /**
    * 注册
    * */
   @PostMapping("/enroll")
    public Result  enroll(@RequestParam String username, @RequestParam String password){
       QueryWrapper<User> wrapper = new QueryWrapper<>();
       wrapper.eq("username", username);
       User users = userMapper.selectOne(wrapper);
       if (users != null) {
           // 用户名存在，执行相应的逻辑
           return Result.error("账号已存在");
       } else {
           User user = new User();
           user.setUsername(username);
           user.setPassword(password);
           LocalDateTime now = LocalDateTime.now();
           // 定义日期时间格式
           DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
           user.setCreatedAt(now.format(formatter));
           userMapper.insert(user);
           return Result.ok("200","注册成功");
       }
   }
   /**
    * 查询所有用户
    * */
    @PostMapping("/select")
   public Result selectuse(){
        List<User> list = userMapper.list();
        return  Result.ok(list);
    }

   @GetMapping("/person")
   public User person(@RequestParam String token) {
       String username = JWTUtil.getUsername(token);
       QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
       userQueryWrapper.eq("username", username);
       return userMapper.selectOne(userQueryWrapper);
   }

   /**
    * 查询所有用户
    * */
   @GetMapping("/user/page")
   public PageInfo<User> getUsers(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit) {
       PageInfo<User> resultPage = new PageInfo<>();
       List<User> users = userMapper.pageList((page - 1) * limit, page * limit);
       Integer number = userMapper.pageCount();
       resultPage.setList(users);
       resultPage.setTotal(number);
       // 可以添加自定义条件查询
       return resultPage;
   }

    /**
     * 修改密码
     */
    @PostMapping("/user/updatePassword")
    public Result updatePassword(@RequestBody Map<String, String> map) {
        String username = JWTUtil.getUsername(map.get("token"));
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username", username);
        User user = userMapper.selectOne(userQueryWrapper);
        if (user != null) {
            user.setPassword(map.get("password"));
            userMapper.updateById(user);
        }
        return Result.ok("200","修改密码成功");
    }

    @PostMapping("/user/updateUser")
    public Result updateUser(@RequestBody User user) {
        userMapper.updateById(user);
        return Result.ok("200","修改信息成功");
    }
}

